// index.js
import {
  get,
  post
} from '../../utils/request'
import {
  APIS
} from '../../utils/api'
import { timestampToTime } from '../../utils/util'
// 获取应用实例
const app = getApp()
const statusMap = ['custom', 'today', 'yestoday', 'week', 'month'];

Page({
  data: {
    startDate: '-',
    endDate: '-',
    rangeStart: '2024-01-01',
    rangeEnd: '1990-01-01',
    currShop: {},
    rangeType: 1,
    dimension: [],
    total: []
  },
  onLoad(options) {
    let {rangeType, start, end} = options;
    let currShop = wx.getStorageSync('currShop');
    this.setData({
      rangeType,
      startDate: start,
      endDate: end,
      currShop,
      rangeEnd: timestampToTime(Date.now())
    }, () => {
      this.getData(rangeType);
    })
  },

  getData(idx) {
    let {currShop, startDate, endDate} = this.data;
    post(`${APIS.statistics}/dimension`, {
      shop: currShop.id,
      start: startDate,
      end: endDate,
      range: statusMap[idx]
    }).then(res => {
      let {dimension, total, date} = res.data;
      this.setData({
        dimension,
        total,
        ...date
      })
    })
  },
  handleShopChange(e) {
    let currShop = e.detail;
    this.setData({
      currShop
    })
    this.getData();
  },
  bindStartChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let date = e.detail.value;
    let endDate = new Date(date) < new Date(this.data.endDate)
      ? this.data.endDate
      : date
    this.setData({
      startDate: date,
      endDate,
    }, () => {
      this.getData(0)
    });
  },
  bindEndChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      endDate: e.detail.value,
    }, () => {
      this.getData(0)
    })

  },
})